package com.example.kaogong.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.dto.PostDto;
import com.example.kaogong.sys.pojo.vo.PostVo;
import com.example.kaogong.sys.service.IPostService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;

/**
 * <p>
 * 帖子 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/post")
@Api(tags = "论坛接口")
@Slf4j
public class PostController {

    @Autowired
    private IPostService postService;

    @ApiOperation(value = "用户发表帖子")
    @PostMapping("")
    public RespBean addPost(@Valid @RequestBody PostDto postDto, Principal principal) {
        if (null == principal)
            return RespBean.error();

        log.info("request:{}", new Gson().toJson(postDto));
        RespBean respBean = postService.addPost(postDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "分页获取所有帖子")
    @GetMapping("/all/{current}/{size}")
    public RespBean<Page<PostVo>> getAllPost(@PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<PostVo>> respBean = postService.getAllPost(current, size);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "分页获取当前登录用户所发帖子")
    @GetMapping("/{current}/{size}")
    public RespBean<Page<PostVo>> getCurrentUserPost(@PathVariable Long current, @PathVariable Long size, Principal principal) {
        if (null == principal)
            return RespBean.error();
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<PostVo>> respBean = postService.getCurrentUserPost(current, size, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据帖子编号删除帖子")
    @DeleteMapping("/{postId}")
    public RespBean deletePostById(@PathVariable Integer postId) {
        Assert.notNull(postId, new ParamsErrorException("帖子编号不能为空"));
        log.info("postId:{}", postId);
        RespBean respBean = postService.deletePostById(postId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
